import { defineStore } from 'pinia'
import { $utils } from '@/utils'
import { useGameStore } from './game'
import { getLotteryTemplate } from '@/api'
import { isEmpty } from 'lodash-es'
import { NUMBER_CLR_MAP, TXT_CLR_MAP } from '@/components/game/tpl-lhc/lhc-constant'
import { cloneDeep } from 'lodash-es'

export const useLhcStore = defineStore('tpl-lhc', {
  state: () => ({
    gameTemplateData: [], //游戏渲染数据
    currentGameType: 0, //当前游戏索引
    sonGameTypeIndex: 0, //子游戏类型索引
    gameStore: useGameStore(),
  }),
  getters: {
    getGameComponentTag() {
      const playType = this.getPlayTypeCodeList[this.currentGameType]

      if (playType === 'LX' || playType === 'LM' || playType === 'ZXBZ' || playType === 'TMZ') {
        return 'SubGame'
      }

      if (playType === 'TX' || playType === 'ZM' || playType === 'TMSB') {
        return 'CommonGame'
      }
    },

    // 六合彩所有游戏玩法
    getPlayTypeCodeList() {
      if (isEmpty(this.gameTemplateData)) return []
      return this.gameTemplateData.map((game) => game.playTypeCode)
    },

    // 获得 选中游戏类型数据
    getCurrentGameData() {
      if ($utils.isEmpty(this.gameTemplateData)) return []
      return this.gameTemplateData[this.currentGameType]
    },

    // 选中子游戏的类型
    getSonGameType() {
      //有子类型就找子类型的 类型
      return this.getCurrentGameData.playTypeCode
    },
  },

  actions: {
    // 入口，初始化
    async init() {
      if (isEmpty(this.gameTemplateData)) {
        await this.fetchGameTemplateData()
      } else {
        this.gameStore.setCurrentSonGame(this.getSonGameType)
      }
    },

    // 当前游戏索引
    setCurrentGameType(index) {
      this.currentGameType = index
      this.gameStore.setCurrentSonGame(this.getSonGameType)
    },

    // 子游戏类型索引
    setSonGameTypeIndex(index) {
      this.sonGameTypeIndex = index
      this.gameStore.setCurrentSonGame(this.getSonGameType)
    },

    // 立即下注
    betNow(refs) {
      refs['gameRef'].betNow()
    },

    // 清空所有下注内容
    clearInvestList(refs) {
      refs['gameRef'].updateInvestList('clear')
    },

    // 删除一个游戏的下注订单
    deleteAnInvest({ data }) {
      data.dom['gameRef'].updateInvestList('del', data)
    },

    // 撤消订单
    withdrawAnOrder() {},

    // 获得游戏渲染数据
    async fetchGameTemplateData() {
      try {
        const { data } = await getLotteryTemplate({ 
          lotteryCode: this.gameStore.currentGame, 
          handicapCode: this.gameStore.currentHandicapCode
         })
        const lianmaList = data.find((item) => item.playTypeName === '连码')?.lotteryPlayTypeList
        //
        const temaList = data.find((item) => item.playTypeName === '特码')?.lotteryPlayTypeList
        temaList.forEach(({ cachePlayList }) => {
          cachePlayList.forEach((item) => {
            item['color'] = NUMBER_CLR_MAP[item.playName]
          })
        })
        const zixuanbuzhongList = data.find((item) => item.playTypeName === '自选不中')?.lotteryPlayTypeList
        //
        const texiaoItem = data.find((item) => item.playTypeName === '特肖')
        texiaoItem.lotteryPlayTypeList = [cloneDeep(texiaoItem)] // console.log(data)
        texiaoItem.cachePlayList = null
        //
        const zhengmaItem = data.find((item) => item.playTypeName === '正码')
        zhengmaItem?.cachePlayList.forEach((item) => {
          item['color'] = NUMBER_CLR_MAP[item.playName]
        })
        zhengmaItem.lotteryPlayTypeList = [cloneDeep(zhengmaItem)] // console.log(data)
        zhengmaItem.cachePlayList = null

        const temaseboList = data.find((item) => item.playTypeName === '特码色波').lotteryPlayTypeList
        temaseboList?.forEach(({ cachePlayList }) => {
          cachePlayList.forEach((item) => {
            const color = Object.keys(TXT_CLR_MAP).find((i) => item.playName.includes(i))
            item['color'] = TXT_CLR_MAP?.[color]
          })
        })

        lianmaList
          ?.find((lmItem) => lmItem.playTypeName === '三中二')
          ?.cachePlayList?.forEach((cacheItem) => {
            cacheItem.maxOdds = cacheItem.odds // 连码  三中二 maxOdds 用 odds
          })

        lianmaList?.forEach((game) => {
          game.cachePlayList = Array.from({ length: 49 }, (_, i) => {
            const number = (i + 1).toString().padStart(2, '0')
            return {
              ...JSON.parse(JSON.stringify(game.cachePlayList[0])),
              number,
              color: NUMBER_CLR_MAP[number],
            }
          })
        })

        zixuanbuzhongList?.forEach((game) => {
          game.cachePlayList = Array.from({ length: 49 }, (_, i) => {
            const number = (i + 1).toString().padStart(2, '0')
            return {
              ...JSON.parse(JSON.stringify(game.cachePlayList[0])),
              number,
              color: NUMBER_CLR_MAP[number],
            }
          })
        })

        this.gameTemplateData = data
        this.gameStore.setCurrentSonGame(this.getSonGameType)
      } catch (e) {
        console.log(e)
      }
    },
  },
})
